/**
 * Created by Administrator on 2017/8/3.
 */
$(document).ready(function () {

    var addressId=null;

    var allAddressTpl=$('#all-address-template').html();
    var allAddressCmp=Handlebars.compile(allAddressTpl);
    $.ajax({
        url:C.interface.allAddress,
        dataType:'json',
        type:'get',
        data:{
            token:C.token
        },
        success:function (response) {
            if (response.result=='success'){
                var data=response.data;
                $('#address-list-box').html(allAddressCmp(data));

                //$('.ab').bind('click',insertAddressId);
                //$('.delete-address').bind('click',deleteAddress);

                $('.ab').click(function () {

                    addressId=$(this).parent().data('id');
                    changeAddress();
                });


                /**
                 *秒杀页面本地存储来获取收货地址
                 *address
                 * */
                var address=null;

                var defaultAddressBox=$('#address-list-box').find('.order-address').eq(0).find('.aui-list-item-inner');
                var addressName=defaultAddressBox.find('.order-address-name ').find('span').eq(0).html();
                var addressTel=defaultAddressBox.find('.order-address-name ').find('span').eq(1).html();
                var addressAddress=defaultAddressBox.find('.order-address-detail').html();


                var address={
                    addressName:addressName,
                    addressTel:addressTel,
                    addressAddress:addressAddress
                };

                var addressStorage=new Storage('defaultAddress');
                addressStorage.add(address);
                //localStorage.setItem('defaultAddress',JSON.stringify(address));

            }else if (response.result=='login'){
                alert('登录过期请重新登录');
                againLogin();
            }else {
                alert(response.errorMsg);
            }
        }
    });

    //利用本地存储修改收货地址。
    function insertAddressId() {
        var addressId=$(this).parent().data('id');
        console.log(addressId);
        //利用本地存储实现查找相对应的地址ID
        if(!window.localStorage){
            //alert("浏览器支持localstorage");
            return false;
        }else{
            var storage=window.localStorage;
            storage.setItem("addressId",addressId);
        }
        //location.href='insertaddress.html';
    }


    /**
     * 提交订单的时候点击修改当前的收货地址
     * 并跳转回确认订单页面
     * changeAddress()
     * */
    
    function changeAddress() {
        $.ajax({
            url:C.interface.changeAddress,
            type:'get',
            dataType:'json',
            data:{
                token:C.token,
                id:addressId
            },
            success:function (response) {
                if (response.result=='success'){
                    window.location.href=document.referrer;

                }else if (response.result=='login'){
                    alert('请重新登录');
                }else {
                    alert(response.errorMsg);
                }
            }

        })
    }
    
    


    /**
     * 利用本地存储实现购物车
     * cartLocal
     * */
    function Storage(key) {
        //添加一项数据
        //value:object
        this.add=function (value) {
            if (value){
                var v=this.get();
                v.push(value);
                this.set(v);
            }
        };
        //设置数据
        //value：array null
        this.set=function (value) {
            if (value){
                var v=JSON.stringify(value);
                localStorage.setItem(key,v);
            }else {
                localStorage.removeItem(key);
            }
        };
        // 获取数据
        this.get=function () {
            var value=localStorage.getItem(key)||'[]';
            return JSON.parse(value);
        }
    }
    
});